﻿using System;
using System.Linq;
using Lbb.Data;
using Lbb.Web.Mvc;
using Lbb.URating.DL;
using Lbb.Common;

namespace Lbb.URating.Models
{
    /// <summary>
    /// 用户评分数据列表
    /// </summary>
    /// <author>Chiron</author>
    public class U_RatingDataList : ExtDataList<U_Rating>
    {
        #region Ctors
        /// <summary>
        /// 构建用户评分数据列表
        /// </summary>
        public U_RatingDataList()
        {
            if (Request != null)
            {
                CategoryIdsStr = Request["categoryIds"];
                ClickIdsStr = Request["clickIds"];
                RatingIdsStr = Request["ratingIds"];
                ObjectTypesStr = Request["objTypes"];
                ObjtypeIdsStr = Request["objIds"];
                MinRating = GetReqDecimal("minRating");
                MaxRating = GetReqDecimal("maxRating");
            }
        }
        #endregion Ctors

        #region Props

        #region CategoryIds
        /// <summary>
        /// 所属分类
        /// </summary>
        public string CategoryIdsStr
        {
            get
            {
                return RouteValues.categoryIds as string;
            }
            set
            {
                RouteValues.categoryIds = value;
                _categoryIdsChanged = true;
            }
        }
        private bool _categoryIdsChanged = false;
        /// <summary>
        /// 所属分类
        /// </summary>
        public Guid[] CategoryIds
        {
            get
            {
                if (_categoryIds == null || _categoryIdsChanged)
                {
                    _categoryIds = CategoryIdsStr.ToGuids(removeEmpty: true);
                    _categoryIdsChanged = false;
                }
                return _categoryIds;
            }
            set
            {
                CategoryIdsStr = value.Merger();
                _categoryIdsChanged = false;
            }
        }
        private Guid[] _categoryIds;
        #endregion CategoryIds

        #region ClickIds
        /// <summary>
        /// 点击器
        /// </summary>
        public string ClickIdsStr
        {
            get
            {
                return RouteValues.clickIds as string;
            }
            set
            {
                RouteValues.clickIds = value;
                _clickIdsChanged = true;
            }
        }
        private bool _clickIdsChanged = false;
        /// <summary>
        /// 点击器
        /// </summary>
        public Guid[] ClickIds
        {
            get
            {
                if (_ratingIds == null || _clickIdsChanged)
                {
                    _ratingIds = ClickIdsStr.ToGuids(removeEmpty: true);
                    _clickIdsChanged = false;
                }
                return _ratingIds;
            }
            set
            {
                ClickIdsStr = value.Merger();
                _clickIdsChanged = false;
            }
        }
        private Guid[] _clickIds;
        #endregion ClickIds

        #region RatingIds
        /// <summary>
        /// 评分项
        /// </summary>
        public string RatingIdsStr
        {
            get
            {
                return RouteValues.ratingIds as string;
            }
            set
            {
                RouteValues.ratingIds = value;
                _ratingIdsChanged = true;
            }
        }
        private bool _ratingIdsChanged = false;
        /// <summary>
        /// 评分项
        /// </summary>
        public Guid[] RatingIds
        {
            get
            {
                if (_ratingIds == null || _ratingIdsChanged)
                {
                    _ratingIds = RatingIdsStr.ToGuids(removeEmpty: true);
                    _ratingIdsChanged = false;
                }
                return _ratingIds;
            }
            set
            {
                RatingIdsStr = value.Merger();
                _ratingIdsChanged = false;
            }
        }
        private Guid[] _ratingIds;
        #endregion ClickIds

        #region ObjectTypes
        /// <summary>
        /// 关联的对象类型
        /// </summary>
        public string ObjectTypesStr
        {
            get
            {
                return RouteValues.objTypes as string;
            }
            set
            {
                RouteValues.objTypes = value;
                objTypesChanged = true;
            }
        }
        private bool objTypesChanged = false;
        /// <summary>
        /// 关联的对象类型
        /// </summary>
        public string[] ObjectTypes
        {
            get
            {
                if (objTypes == null || objTypesChanged)
                {
                    objTypes = ObjectTypesStr.Split(removeEmpty: true);
                }
                return objTypes;
            }
            set
            {
                ObjectTypesStr = value.Merger();
                objTypesChanged = false;
            }
        }
        private string[] objTypes;
        #endregion ObjectTypes

        #region ObjtypeIds
        /// <summary>
        /// 关联对象
        /// </summary>
        public string ObjtypeIdsStr
        {
            get
            {
                return RouteValues.objIds as string;
            }
            set
            {
                RouteValues.objIds = value;
                _objIdsChanged = true;
            }
        }
        private bool _objIdsChanged = false;
        /// <summary>
        /// 关联对象
        /// </summary>
        public string[] ObjtypeIds
        {
            get
            {
                if (_objIds == null || _objIdsChanged)
                {
                    _objIds = ClickIdsStr.Split(removeEmpty: true);
                    _objIdsChanged = false;
                }
                return _objIds;
            }
            set
            {
                ClickIdsStr = value.Merger();
                _objIds = value;
                _objIdsChanged = false;
            }
        }
        private string[] _objIds;
        #endregion ObjtypeIds

        /// <summary>
        /// 评分下限
        /// </summary>
        public decimal? MinRating
        {
            get
            {
                return RouteValues.minRating as decimal?;
            }
            set
            {
                RouteValues.minRating = value;
            }
        }
        /// <summary>
        /// 评分上限
        /// </summary>
        public decimal? MaxRating
        {
            get
            {
                return RouteValues.maxRating as decimal?;
            }
            set
            {
                RouteValues.maxRating = value;
            }
        }
        #endregion Props

        #region Methods
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="data">数据源</param>
        public void FillData(IQueryable<U_Rating> data)
        {
            if (data == null) throw new ArgumentNullException("data");

            switch (OrderBy)
            {
                case "rating_up":
                    data = data.OrderBy(d => d.Rating);
                    break;
                case "rating_down":
                    data = data.OrderByDescending(d => d.Rating);
                    break;
                case "username_up":
                    data = data.OrderBy(d => d.Username);
                    break;
                case "username_down":
                    data = data.OrderByDescending(d => d.Username);
                    break;
                case "clickTitle_up":
                    data = data.OrderBy(d => d.U_ClickRating.U_Click.Title);
                    break;
                case "clickTitle_down":
                    data = data.OrderByDescending(d => d.U_ClickRating.U_Click.Title);
                    break;
            }

            Items = data.GetPartialData(FromIndex, PageSize, out Total);
        }
        #endregion Methods
    }
}
